<?php
$dbname = 'casa3gweb';
$dbhost = 'cougar.wou.edu';
$dbuser = 'casa3gweb';
$dbpass = 'casa';
$db = mysql_connect($dbhost,$dbuser,$dbpass);

$userLevelArray = array (
					'1' => 'Super Admin',
					'2' => 'Program Director'
					);		
					
function getUsers() {
	global $userLevelArray, $stringJSON, $dbname, $db, $db_selected;
	require_once("../classes/JSON.php");
	
	$sql = "SELECT CU.UID,CU.Userid,CU.Password,CU.ProgramID,CU.NameFirst,CU.NameLast,CU.Email,CU.Phone1,CU.AdminLevel,
			CP.ProgramName
			FROM casa_users CU
			LEFT JOIN casa_programs CP ON CP.PID = CU.ProgramID
			WHERE CU.AdminLevel > 0 ORDER BY CU.NameLast ASC";
	$result = mysql_db_query($dbname,$sql);
	if (mysql_num_rows($result)>0) {
		$stringJSON = '({"Users":[';
		while ($value = mysql_fetch_assoc($result)) {
			if ($value[Userid] == '0') {
				$userid = '';
			} else {
				$userid = $value[Userid];
			}
			if ($value[Password] == '0') {
				$password = '';
			} else {
				$password = $value[Password];
			}
			if ($value[Email] == '0') {
				$email = '';
			} else {
				$email = $value[Email];
			}
			if ($value[Phone1] == 0) {
				$phone = '';
			} else {
				$phone = $value[Phone1];
			}
			$newArray = array(
							'UID'        => $value[UID],
							'Delete'     => '<a href="#" onClick="confirmDelete(\''.$value[UID].'\',\'user\'); return false;" title="Delete"><img src="images/buttonDeleteBig.gif" style="border: none;" alt="Delete" /></a>',
							'Userid'     => $userid,
							'Password'   => $password,
							'AdminLevel' => $userLevelArray[$value[AdminLevel]],
							'NameFirst'  => $value[NameFirst],
							'NameLast'   => $value[NameLast],
							'Email'      => $email,
							'ProgramName'=> $value[ProgramName],
							'Phone1'     => $phone
							);
			
			$json = new Services_JSON();
			$stringJSON .= $json->encode($newArray).',';
		}	
		// Strip last ,
		$strLen = strlen($stringeJSON);
		$stringJSON = substr($stringJSON,0,($strLen-1));
		$stringJSON .= ']})';
	} else {
		$stringJSON = 'There are no users yet.';
	}
	
	return $stringJSON;
}

function deleteUser($uID) {
	global $dbname, $db, $db_selected;

	$sql = "DELETE from casa_users WHERE UID = '$uID'";
	$result = mysql_db_query($dbname,$sql);
	return $uID;
}

function editUser($phpString) {
	global $userLevelArray,$dbname, $db, $db_selected;
	
	// Parse phpString
	$phpArray = explode('|',$phpString);
	$uID       = $phpArray[0];
	$value     = fieldToDB($phpArray[1]);
	$dbField   = $phpArray[2];
	$gridRow   = $phpArray[3];
	$origValue = $phpArray[4];
	
	if ($dbField == 'AdminLevel') {
		$flipUserLevelArray = array_flip($userLevelArray);
		$value = $flipUserLevelArray[$value];
	} elseif ($dbField == 'Program') {
		// Get programID from program name
		$sql = "SELECT PID from casa_programs WHERE ProgramName = '$value'";
		$result = mysql_db_query($dbname,$sql);
		$row = mysql_fetch_assoc($result);
		$value   = $row[PID];
		$dbField = 'ProgramID';
	}

	if (!empty($uID)) {
		if ( ($dbField == 'AdminLevel') && ($value == 2) ) {
			$sql = "UPDATE casa_users SET 
						$dbField   = '$value',
						PositionID = 3
					WHERE UID = '$uID'";
		} else {
			$sql = "UPDATE casa_users SET $dbField = '$value' WHERE UID = '$uID'";
		}	
		$result = mysql_db_query($dbname,$sql);
	}
}	

function saveUser($phpString) {
	global $dbname, $db, $db_selected;
	
	// Parse phpString
	$phpArray = explode('|',$phpString);
	
	$nameFirst  = $phpArray[0];
	$nameLast   = $phpArray[1];
	$email      = $phpArray[2];
	$userid     = $phpArray[3];
	$password   = $phpArray[4];
	$adminLevel = $phpArray[5];
	$programID  = $phpArray[6];
	
	$sql = "INSERT into casa_users (
			Userid,
			Password,
			ProgramID,
			NameFirst,
			NameLast,
			Email,
			AdminLevel,
			DateAdded
			) values (
			'$userid',
			'$password',
			'$programID',
			'$nameFirst',
			'$nameLast',
			'$email',
			'$adminLevel',
			'".date('Y-m-d')."'
			)";
	$result = mysql_db_query($dbname,$sql);
}

if ($_GET[getUsers]==1) {
	$stringJSON = getUsers();
	echo $stringJSON;
}

?>